<?php 
/**
 * @version		$Id: birtfiller.php 2011.08.11 thierry $
 * @package		site.models
 * @subpackage	com_projid
 * @copyright	Copyright (C) 2011 Pojid All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */
defined('_JEXEC') or die('Restricted access');
 class BirtFiller{

		private $report;
		public $result ;
		private $queries = array();
		private $RP_result;
		private $recup;
	
	public function __construct($report){
		$this->report =$report;
		$this->setResult();
		$this->getUrlContent();
		}

//recupère les valeur passé dans l'url
public function getUrlContent(){
		$keys = array();
		$values = array();
		$RP_result = array();
	foreach($this->result as $entry){
		$name = $entry->param_name ;
		if(array_key_exists($name,$_GET)){
			${$name}= $_GET[$name];
			array_push($keys,$name);
			array_push($values,${$name});
			}else{
			${$name} = "";
			}
		}
		if($keys != null && $values != null){
				$RP_result = array_combine($keys,$values);
				}else{
				$RP_result = null;
				}
		$this->RP_result = $RP_result;
		}

//Récupère les variables disponible dans la base de donnée et complète un tableau
	public function setResult(){
		$report = $this->report;

		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('param_name,param_desc,param_type, requete_id, param_default');
		$query->from('#__projid_birt');
		$query->leftjoin('#__projid_report_param on #__projid_birt.id_birt=#__projid_report_param.report_id');
		$query->leftjoin('#__projid_birt_parameter on #__projid_report_param.param_id=#__projid_birt_parameter.id_param');
	
		$query->where('#__projid_birt.birt_name="'.$report.'"');
		$query->order('#__projid_report_param.ordering');
		$db->setQuery((string)$query);

		$entries = $db->loadObjectList();
		$this->result=$entries;

		}

//recupère les requete de la base projid
public function setOption($object){
	$requete = $object->requete_id;
	$urlcontent = $this->RP_result;
	
	$MyDB = new Mydb($requete);
	$myresult = $MyDB->getResult();
	$key = $object->param_name;

	$default = $object->param_default;
	
	//Change string into php variable 
	//$myrp ='$'.$name;
	//eval("\$myrp = \"$myrp\";");
	$myrp = $urlcontent[$key];
	$result="";
	foreach( $myresult as $entry){
	if(!isset($entry[1]) OR $entry[1]==null){
			$entry[1]=$entry[0];
	}
	if(isset($entry[0]) AND $entry[0] == $myrp||($myrp==null && $entry[0]==$default)){
		$result.='
	<option value ="'.$entry[0].'" selected="selected">'.$entry[1].'</option>'
	;
		}else{
		$result.=
	'<option value ="'.$entry[0].'">'.$entry[1].'</option>'
	;
		}///end foreach
		
		}//end foreach
return $result;
	}//end function
	
//fonction de creation du corp du formulaire
	private function setBodyForm($export){

	$body_h ='<tr>';
	foreach($this->result as $entry){
	$name = $entry->param_name ;
	
		$body_h .= '<td class="article-info article-info-term">';
		//$body_h .= $this->transVar($entry->param_name);
		$body_h .=$entry->param_desc;
		$body_h .= '</td>';
		}
	$body_h .='<td>&nbsp;</td><td rowspan="2" style="padding-left:25px;">'.$export.'</td></tr>';

	$body_b ='<tr>';
	
	foreach($this->result as $entry){
		$body_b .= '<td>';
		$body_b .='<select name="'.$entry->param_name.'">';
		$body_b .=$this->setOption($entry);
		$body_b .='</select>';
		$body_b .= '</td>';
		}
	
	$body_b .='<td> <input type="submit" value="Ok"> </td>';
	$body_b .='</tr>';

	$body_form=$body_h;
	$body_form.=$body_b;
	return $body_form;
}

//fonction de creation du formulaire
	public function setForm($export){
	$form_head = <<<EOF
<DIV id="form"> 
        <form action="" name="int-php.iframe"> 
		<table> 
EOF;
	$form_foot = <<<EOF
</table> 
</form> 
<DIV>
EOF;

	$form = $form_head;
	$form .= $this->setBodyForm($export);
	$form .=$form_foot;

	return $form;
	}
	
 //Get the parameter for the pdf and the xls generation
public function getParam(){
	$result = $this->result;
		$urlcontent = $this->RP_result;
		$param="";
		foreach($result as $entry){
		 $name = $entry->param_name ;
		 $type = $entry->param_type;
		$default = $entry->param_default;
if(!isset($urlcontent[$name]) OR $urlcontent[$name] == null){ $urlcontent[$name] = $default; }
$param .= '&amp;'.$name.'='.$urlcontent[$name];
		}
	return $param;
	}
	
	
	
//insert code between $tast and task option
	public function setParameter($task){
		$result = $this->result;
		$urlcontent = $this->RP_result;
	
		foreach($result as $entry){
		 $name = $entry->param_name ;
		 $type = $entry->param_type;
		$default = $entry->param_default;
		//$myrp ='$'.$name;
		//eval("\$myrp = \"$myrp\";");
		//$myrp = $urlcontent[$name];
	if(!isset($urlcontent[$name]) OR $urlcontent[$name] == null){ $urlcontent[$name] = $default; }

$task->setParameterValue($name, new java("java.lang.".ucfirst($type)."",$urlcontent[$name]));

		}//end foreach
return $task;
	}//end Fonction
 }
?>